package com.example.demo.kafka.consumer;

import com.example.demo.domain.Message;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 类描述:
 *
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2018/12/7 18:57
 **/
@Slf4j
@Component
public class Consumer {
    @KafkaListener(topics = {"wjwei"})
    public void listener(ConsumerRecord<?,?> record){
        Optional<?> message = Optional.ofNullable(record);
        if (message.isPresent()){
            String topic = record.topic();
            String key = record.key().toString();
            String value = record.value().toString();
            Message message1 = new Gson().fromJson(value,Message.class);
            log.info("----------------- topic =" + topic);
            log.info("------------------ key =" + key);
            log.info("------------------ value =" + message1);
        }
    }
}
